package com.ncmmall.application.business.shoppingcart;

import com.ncmmall.application.business.goods.GoodsApplication;
import com.feijin.commons.exceptions.InterfaceException;
import com.feijin.commons.lang.ArithUtils;
import com.ncmmall.domain.business.goods.sku.GoodsSku;
import com.ncmmall.domain.business.goods.unit.GoodsUnit;
import com.ncmmall.domain.business.shoppingcart.QShoppingCart;
import com.ncmmall.domain.business.shoppingcart.ShoppingCart;
import com.ncmmall.domain.business.shoppingcart.ShoppingCartRepository;
import com.ncmmall.domain.security.user.WebUser;
import com.ncmmall.querychannel.service.QueryChannelService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author linxs
 * @Description: 购物车业务
 * @Date: 2019-07-14 20:14
 */
@Service
@Transactional
public class ShoppingCartApplication {

    @Autowired
    private ShoppingCartRepository shoppingCartRepository;

    @Autowired
    private GoodsApplication goodsApplication;

    @Autowired
    private QueryChannelService queryChannelService;


    public void modifySupplierQuantity(ShoppingCart cart, int quantity, GoodsUnit unit, int unitQuantity) {

        if (cart.getSku().getStore() < quantity) {
            throw new InterfaceException("库存不足");
        }

        cart.setQuantity(quantity);
        cart.setSubtotalPrice(ArithUtils.mul(goodsApplication.getSupplierLeverPrice(cart.getSku(), quantity), quantity));
        cart.setUnit(unit);
        cart.setUnitQuantity(unitQuantity);


        shoppingCartRepository.save(cart);
    }

    /**
     * 添加商品到进货车（采购端）
     *
     * @param member
     * @param skus
     * @param units
     * @param quantitys
     * @param unitQuantitys
     */
    public void addGoodsToShoppingCartOfSupplier(WebUser member, List<GoodsSku> skus, List<GoodsUnit> units, List<Integer> quantitys, List<Integer> unitQuantitys) {

        for (int i = 0; i < skus.size(); i++) {

            GoodsSku sku = skus.get(i);

            QShoppingCart qShoppingCart = QShoppingCart.shoppingCart;
            ShoppingCart existShoppingCart = queryChannelService.findOne(qShoppingCart.member.eq(member).and(qShoppingCart.sku.eq(sku)), ShoppingCart.class);

            if (null != existShoppingCart) {

                existShoppingCart.setQuantity(existShoppingCart.getQuantity() + quantitys.get(i));
                existShoppingCart.setUnitQuantity(existShoppingCart.getUnitQuantity() + quantitys.get(i));

                if (sku.getStore() < existShoppingCart.getQuantity()) {
                    throw new InterfaceException("库存不足");
                }

                existShoppingCart.setSubtotalPrice(ArithUtils.mul(goodsApplication.getSupplierLeverPrice(sku, existShoppingCart.getQuantity()), existShoppingCart.getQuantity()));
                shoppingCartRepository.save(existShoppingCart);
                continue;
            }

            if (sku.getStore() < quantitys.get(i)) {
                throw new InterfaceException("库存不足");
            }

            ShoppingCart shoppingCart = new ShoppingCart();
            shoppingCart.setMember(member);
            shoppingCart.setGoodsImage(sku.getGoods().getDefaultImage());
            shoppingCart.setGoodsName(sku.getGoods().getName());
            shoppingCart.setSku(sku);
            shoppingCart.setQuantity(quantitys.get(i));
            shoppingCart.setUnit(sku.getGoods().getUnit());
            shoppingCart.setUnitQuantity(quantitys.get(i));
            shoppingCart.setSubtotalPrice(ArithUtils.mul(goodsApplication.getSupplierLeverPrice(sku, quantitys.get(i)), quantitys.get(i)));
            shoppingCart.setSkuNames(StringUtils.replace(sku.getSkuNames(), ",", " "));

            shoppingCartRepository.save(shoppingCart);

        }

    }

    /**
     * 添加商品到购物车
     *
     * @param sku
     * @param quantity
     */
    public ShoppingCart addGoodsToShoppingCart(WebUser member, GoodsSku sku, int quantity) {

        QShoppingCart qShoppingCart = QShoppingCart.shoppingCart;
        ShoppingCart existShoppingCart = queryChannelService.findOne(qShoppingCart.member.eq(member).and(qShoppingCart.sku.eq(sku)), ShoppingCart.class);

        if (null != existShoppingCart) {

            if (sku.getGoods().getMarketingType() == 0) {
                if ((existShoppingCart.getQuantity() + quantity) > sku.getStore()) {
                    throw new InterfaceException("库存不足");
                }
            }

            //特价
            if (sku.getGoods().getMarketingType() == 1) {
                if ((existShoppingCart.getQuantity() + quantity) > sku.getBargainStore()) {
                    throw new InterfaceException("库存不足");
                }
            }

            //秒杀库存
            if (sku.getGoods().getMarketingType() == 2) {
                if ((existShoppingCart.getQuantity() + quantity) > sku.getSeckillStore()) {
                    throw new InterfaceException("库存不足");
                }

                if ((existShoppingCart.getQuantity() + quantity) > sku.getSeckillLimit()) {
                    throw new InterfaceException("超过限购数量");
                }
            }


            existShoppingCart.setQuantity(existShoppingCart.getQuantity() + quantity);
            existShoppingCart.setSubtotalPrice(ArithUtils.mul(sku.getPrice(), existShoppingCart.getQuantity()));
            shoppingCartRepository.save(existShoppingCart);
            return existShoppingCart;
        }

        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setMember(member);
        shoppingCart.setGoodsImage(sku.getGoods().getDefaultImage());
        shoppingCart.setGoodsName(sku.getGoods().getName());
        shoppingCart.setSku(sku);
        shoppingCart.setQuantity(quantity);

        //普通商品
        if (sku.getGoods().getMarketingType() == 0) {

            if (quantity > sku.getStore()) {
                throw new InterfaceException("库存不足");
            }

            if (sku.getGoods().getIsSupplier() == 0) {
                shoppingCart.setSubtotalPrice(ArithUtils.mul(sku.getPrice(), quantity));
            } else { //供应商商品阶梯等级价格
                shoppingCart.setSubtotalPrice(ArithUtils.mul(goodsApplication.getSupplierLeverPrice(sku, quantity), quantity));
            }

        }

        //秒杀商品
        if (sku.getGoods().getMarketingType() == 2) {

            if (sku.getGoods().getSeckillStatus() == 5 && sku.getGoods().getSeckillSession().getStatus() == 2) {    //活动已开始
                shoppingCart.setSubtotalPrice(ArithUtils.mul(sku.getSeckillPrice(), quantity));
            } else {
                shoppingCart.setSubtotalPrice(ArithUtils.mul(sku.getPrice(), quantity));
            }

        }

        //特价商品
        if (sku.getGoods().getMarketingType() == 1) {

            if (sku.getGoods().getBargainStatus() == 5) {    //活动已开始
                shoppingCart.setSubtotalPrice(ArithUtils.mul(sku.getBargainPrice(), quantity));
            } else {
                shoppingCart.setSubtotalPrice(ArithUtils.mul(sku.getPrice(), quantity));
            }

        }

        shoppingCart.setSkuNames(StringUtils.replace(sku.getSkuNames(), ",", " "));

        shoppingCartRepository.save(shoppingCart);

        return shoppingCart;

    }

    /**
     * 修改数据
     *
     * @param cart
     * @param quantity
     */
    public void modifyQuantity(ShoppingCart cart, int quantity) {

        cart.setQuantity(quantity);

        if (cart.getSku().getGoods().getIsSupplier() == 1) {
            cart.setSubtotalPrice(ArithUtils.mul(goodsApplication.getSupplierLeverPrice(cart.getSku(), quantity), quantity));
        } else {
            cart.setSubtotalPrice(ArithUtils.mul(cart.getSku().getPrice(), quantity));
        }

        shoppingCartRepository.save(cart);
    }

    /**
     * 批量删除购物车内的商品
     *
     * @param carts
     */
    public void removes(List<ShoppingCart> carts) {
        shoppingCartRepository.delete(carts);
    }
}
